﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SenerPro;

namespace MensUnoShop.Models
{
    class DBMarket
    {
        private DataClassesMenDataContext _dc;
        private Logger _log = new Logger(Config.logpath);

        /// <summary>
        /// 取得該tag下的所有賣場
        /// </summary>
        /// <param name="tagid"></param>
        /// <returns></returns>
        public List<SimpleMarket> GetMarketList(long tagid)
        {
            List<SimpleMarket> smlist = new List<SimpleMarket>();
            try
            {

                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var mtlist = from mt in _dc.V_MarketTagList
                                 join tr in _dc.V_TagMarketTree on mt.MKguid equals tr.vmkguid
                                 where tr.vtid1 == tagid || tr.vtid2 == tagid
                                 select new { mt.MKguid, mt.MKedate, mt.MKsdate, mt.MKprice, mt.MKshowPrice, mt.MKtitle, mt.TMtagid, mt.MKcolortagid, mt.MKgrouptagid, mt.MKsn, mt.MKtype };
                    //var mtlist = _dc.V_MarketTagList.Where(m => m.TMtagid == tagid);
                    if (mtlist != null)
                    {
                        foreach (var mt in mtlist)
                        {
                            SimpleMarket sm = new SimpleMarket()
                            {
                                MKGUID = mt.MKguid,
                                EDATE = mt.MKedate,
                                SDATE = mt.MKsdate,
                                PRICE = mt.MKprice,
                                SHOPPRICE = mt.MKshowPrice,
                                TITLE = mt.MKtitle,
                                STATUS = true,
                                TAGID = mt.TMtagid,
                                COLORTAGID = mt.MKcolortagid.HasValue ? mt.MKcolortagid.Value : 0,
                                GROUPTAGID = mt.MKgrouptagid.HasValue ? mt.MKgrouptagid.Value : 0,
                                SN = mt.MKsn.Trim(),
                                TYPE = (Config.MarketType)mt.MKtype
                            };
                            sm.COLORTITLE = sm.COLORTAGID == 0 ? string.Empty : _dc.UTag.FirstOrDefault(ss => ss.TAGid == mt.MKcolortagid).TAGtitle;
                            sm.GROUPTITLE = sm.GROUPTAGID == 0 ? string.Empty : _dc.UTag.FirstOrDefault(ss => ss.TAGid == mt.MKgrouptagid).TAGtitle;

                            var pdlist = _dc.UProduct.Where(pp => pp.PDmkguid == mt.MKguid);
                            List<Product> plist = new List<Product>();
                            if (pdlist != null)
                            {
                                foreach (var p in pdlist)
                                {
                                    Product pt = new Product()
                                    {
                                        PDGUID = p.PDguid,
                                        PICBIG = p.PDpicbig,
                                        PICSMALL = p.PDpicsmall,
                                    };
                                    var t = _dc.UTag.SingleOrDefault(ss => ss.TAGid == p.PDtitletagid && ss.TAGtype == (int)Config.TagType.PRODUCT);
                                    if (t != null)
                                    {
                                        pt.TITLE = t.TAGtitle;
                                        pt.TITLETAGID = (int)t.TAGparentid.Value;
                                    }
                                    plist.Add(pt);
                                }
                            }
                            if (plist.FirstOrDefault() != null)
                                sm.IMG = plist.FirstOrDefault().PICBIG;                            
                            sm.PRODUCTLIST = plist;
                            smlist.Add(sm);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetMarketList", ex.ToString());
            }
            return smlist;
        }
        public List<SimpleMarket> GetMarketList(long tagid, DateTime date)
        {
            List<SimpleMarket> smlist = new List<SimpleMarket>();
            try
            {

                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var mtlist = from mt in _dc.V_MarketTagList
                                 join tr in _dc.V_TagMarketTree on mt.MKguid equals tr.vmkguid
                                 where (tr.vtid1 == tagid || tr.vtid2 == tagid) && mt.MKsdate<=date && mt.TMtagid==tagid
                                 select new { mt.MKguid, mt.MKedate, mt.MKsdate, mt.MKprice, mt.MKshowPrice, mt.MKtitle, mt.TMtagid, mt.MKcolortagid, mt.MKgrouptagid, mt.MKsn,mt.MKtype };
                    //var mtlist = _dc.V_MarketTagList.Where(m => m.TMtagid == tagid);
                    if (mtlist != null)
                    {
                        foreach (var mt in mtlist)
                        {
                            SimpleMarket sm = new SimpleMarket()
                            {
                                MKGUID = mt.MKguid,
                                EDATE = mt.MKedate,
                                SDATE = mt.MKsdate,
                                PRICE = mt.MKprice,
                                SHOPPRICE = mt.MKshowPrice,
                                TITLE = mt.MKtitle,
                                STATUS = true,
                                TAGID = mt.TMtagid,
                                COLORTAGID = mt.MKcolortagid.HasValue ? mt.MKcolortagid.Value : 0,
                                GROUPTAGID = mt.MKgrouptagid.HasValue ? mt.MKgrouptagid.Value : 0,
                                SN = mt.MKsn.Trim(),
                                TYPE = (Config.MarketType)mt.MKtype
                            };
                            sm.COLORTITLE = sm.COLORTAGID == 0 ? string.Empty : _dc.UTag.FirstOrDefault(ss => ss.TAGid == mt.MKcolortagid).TAGtitle;
                            sm.GROUPTITLE = sm.GROUPTAGID == 0 ? string.Empty : _dc.UTag.FirstOrDefault(ss => ss.TAGid == mt.MKgrouptagid).TAGtitle;

                            var pdlist = _dc.UProduct.Where(pp => pp.PDmkguid == mt.MKguid);
                            List<Product> plist = new List<Product>();
                            if (pdlist != null)
                            {
                                foreach (var p in pdlist)
                                {
                                    Product pt = new Product()
                                    {
                                        PDGUID = p.PDguid,
                                        PICBIG = p.PDpicbig,
                                        PICSMALL = p.PDpicsmall,
                                    };
                                    var t = _dc.UTag.SingleOrDefault(ss => ss.TAGid == p.PDtitletagid && ss.TAGtype == (int)Config.TagType.PRODUCT);
                                    pt.TITLE = t.TAGtitle;
                                    pt.TITLETAGID = (int)t.TAGparentid.Value;
                                    plist.Add(pt);
                                }

                            }
                            if (plist.Count > 0)
                            {
                                sm.IMG = plist.FirstOrDefault().PICBIG;
                                sm.PRODUCTLIST = plist;
                                smlist.Add(sm);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetMarketList", ex.ToString());
            }
            return smlist;
        }

        public List<SimpleMarket> GetMarketList(long tagid, int amount)
        {
            List<SimpleMarket> smlist = new List<SimpleMarket>();
            try
            {

                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var mtlist = (from mt in _dc.V_MarketTagList
                                 join tr in _dc.V_TagMarketTree on mt.MKguid equals tr.vmkguid
                                 where tr.vtid1 == tagid || tr.vtid2 == tagid
                                 orderby _dc.Random()
                                 select new { mt.MKguid, mt.MKedate, mt.MKsdate, mt.MKprice, mt.MKshowPrice, mt.MKtitle, mt.TMtagid, mt.MKcolortagid, mt.MKgrouptagid, mt.MKsn,mt.MKtype }).Take(amount);
                    //var mtlist = _dc.V_MarketTagList.Where(m => m.TMtagid == tagid);
                    if (mtlist != null)
                    {
                        foreach (var mt in mtlist)
                        {
                            SimpleMarket sm = new SimpleMarket()
                            {
                                MKGUID = mt.MKguid,
                                EDATE = mt.MKedate,
                                SDATE = mt.MKsdate,
                                PRICE = mt.MKprice,
                                SHOPPRICE = mt.MKshowPrice,
                                TITLE = mt.MKtitle,
                                STATUS = true,
                                TAGID = mt.TMtagid,
                                COLORTAGID = mt.MKcolortagid.HasValue ? mt.MKcolortagid.Value : 0,
                                GROUPTAGID = mt.MKgrouptagid.HasValue ? mt.MKgrouptagid.Value : 0,
                                SN = mt.MKsn.Trim(),
                                TYPE = (Config.MarketType)mt.MKtype
                            };
                            sm.COLORTITLE = sm.COLORTAGID == 0 ? string.Empty : _dc.UTag.FirstOrDefault(ss => ss.TAGid == mt.MKcolortagid).TAGtitle;
                            sm.GROUPTITLE = sm.GROUPTAGID == 0 ? string.Empty : _dc.UTag.FirstOrDefault(ss => ss.TAGid == mt.MKgrouptagid).TAGtitle;

                            var pdlist = _dc.UProduct.Where(pp => pp.PDmkguid == mt.MKguid);
                            List<Product> plist = new List<Product>();
                            if (pdlist != null)
                            {
                                foreach (var p in pdlist)
                                {
                                    Product pt = new Product()
                                    {
                                        PDGUID = p.PDguid,
                                        PICBIG = p.PDpicbig,
                                        PICSMALL = p.PDpicsmall,
                                    };
                                    var t = _dc.UTag.SingleOrDefault(ss => ss.TAGid == p.PDtitletagid && ss.TAGtype == (int)Config.TagType.PRODUCT);
                                    pt.TITLE = t.TAGtitle;
                                    pt.TITLETAGID = (int)t.TAGparentid.Value;
                                    plist.Add(pt);
                                }

                            }
                            if (plist.Count > 0)
                            {
                                sm.IMG = plist.FirstOrDefault().PICBIG;
                                sm.PRODUCTLIST = plist;
                                smlist.Add(sm);
                            }

                            
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetMarketList", ex.ToString());
            }
            return smlist;
        }
        public List<SimpleMarket> GetMarketList(long tagid, int amount, DateTime date)
        {
            List<SimpleMarket> smlist = new List<SimpleMarket>();
            try
            {

                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var mtlist = (from mt in _dc.V_MarketTagList
                                  join tr in _dc.V_TagMarketTree on mt.MKguid equals tr.vmkguid
                                  where (tr.vtid1 == tagid || tr.vtid2 == tagid) && mt.MKsdate<=date
                                  orderby _dc.Random()
                                  select new { mt.MKguid, mt.MKedate, mt.MKsdate, mt.MKprice, mt.MKshowPrice, mt.MKtitle, mt.TMtagid, mt.MKcolortagid, mt.MKgrouptagid, mt.MKsn,mt.MKtype }).Take(amount);
                    //var mtlist = _dc.V_MarketTagList.Where(m => m.TMtagid == tagid);
                    if (mtlist != null)
                    {
                        foreach (var mt in mtlist)
                        {
                            SimpleMarket sm = new SimpleMarket()
                            {
                                MKGUID = mt.MKguid,
                                EDATE = mt.MKedate,
                                SDATE = mt.MKsdate,
                                PRICE = mt.MKprice,
                                SHOPPRICE = mt.MKshowPrice,
                                TITLE = mt.MKtitle,
                                STATUS = true,
                                TAGID = mt.TMtagid,
                                COLORTAGID = mt.MKcolortagid.HasValue ? mt.MKcolortagid.Value : 0,
                                GROUPTAGID = mt.MKgrouptagid.HasValue ? mt.MKgrouptagid.Value : 0,
                                SN = mt.MKsn.Trim(),
                                TYPE = (Config.MarketType)mt.MKtype
                            };
                            sm.COLORTITLE = sm.COLORTAGID == 0 ? string.Empty : _dc.UTag.FirstOrDefault(ss => ss.TAGid == mt.MKcolortagid).TAGtitle;
                            sm.GROUPTITLE = sm.GROUPTAGID == 0 ? string.Empty : _dc.UTag.FirstOrDefault(ss => ss.TAGid == mt.MKgrouptagid).TAGtitle;

                            var pdlist = _dc.UProduct.Where(pp => pp.PDmkguid == mt.MKguid);
                            List<Product> plist = new List<Product>();
                            if (pdlist != null)
                            {
                                foreach (var p in pdlist)
                                {
                                    Product pt = new Product()
                                    {
                                        PDGUID = p.PDguid,
                                        PICBIG = p.PDpicbig,
                                        PICSMALL = p.PDpicsmall,
                                    };
                                    var t = _dc.UTag.SingleOrDefault(ss => ss.TAGid == p.PDtitletagid && ss.TAGtype == (int)Config.TagType.PRODUCT);
                                    pt.TITLE = t.TAGtitle;
                                    pt.TITLETAGID = (int)t.TAGparentid.Value;
                                    plist.Add(pt);
                                }
                            }
                            sm.IMG = plist.FirstOrDefault().PICBIG;
                            sm.PRODUCTLIST = plist;
                            smlist.Add(sm);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetMarketList", ex.ToString());
            }
            return smlist;
        }


        public List<SimpleMarket> GetMarketFreeList(long c1id, long c2id, DateTime dateTime)
        {
            List<SimpleMarket> smlist = new List<SimpleMarket>();
            try
            {

                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    
                    
                    var mtlist = from mt in _dc.V_MarketTagList 
                                  join vm in _dc.V_TagMarketTree on mt.MKguid equals vm.vmkguid
                                 where mt.MKsdate <= dateTime && mt.MKedate >= dateTime && vm.vtid1 == c1id && mt.TMtagid == c2id
                                  select new { mt.MKguid, mt.MKedate, mt.MKsdate, mt.MKprice, mt.MKshowPrice, mt.MKtitle, mt.TMtagid, mt.MKcolortagid, mt.MKgrouptagid, mt.MKsn, mt.MKtype };
                    if (mtlist != null)
                    {
                        foreach (var mt in mtlist)
                        {
                            SimpleMarket sm = new SimpleMarket()
                            {
                                MKGUID = mt.MKguid,
                                EDATE = mt.MKedate,
                                SDATE = mt.MKsdate,
                                PRICE = mt.MKprice,
                                SHOPPRICE = mt.MKshowPrice,
                                TITLE = mt.MKtitle,
                                STATUS = true,
                                TAGID = mt.TMtagid,
                                COLORTAGID = mt.MKcolortagid.HasValue ? mt.MKcolortagid.Value : 0,
                                GROUPTAGID = mt.MKgrouptagid.HasValue ? mt.MKgrouptagid.Value : 0,
                                SN = mt.MKsn.Trim(),
                                TYPE = (Config.MarketType)mt.MKtype
                            };
                            sm.COLORTITLE = sm.COLORTAGID == 0 ? string.Empty : _dc.UTag.FirstOrDefault(ss => ss.TAGid == mt.MKcolortagid).TAGtitle;
                            sm.GROUPTITLE = sm.GROUPTAGID == 0 ? string.Empty : _dc.UTag.FirstOrDefault(ss => ss.TAGid == mt.MKgrouptagid).TAGtitle;

                            var pdlist = _dc.UProduct.Where(pp => pp.PDmkguid == mt.MKguid);
                            List<Product> plist = new List<Product>();
                            if (pdlist != null)
                            {
                                foreach (var p in pdlist)
                                {
                                    Product pt = new Product()
                                    {
                                        PDGUID = p.PDguid,
                                        PICBIG = p.PDpicbig,
                                        PICSMALL = p.PDpicsmall,
                                    };
                                    var t = _dc.UTag.SingleOrDefault(ss => ss.TAGid == p.PDtitletagid && ss.TAGtype == (int)Config.TagType.PRODUCT);
                                    pt.TITLE = t.TAGtitle;
                                    pt.TITLETAGID = (int)t.TAGparentid.Value;
                                    plist.Add(pt);
                                }
                            }
                            sm.IMG = plist.FirstOrDefault().PICBIG;
                            sm.PRODUCTLIST = plist;
                            smlist.Add(sm);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetMarketFreeList", ex.ToString());
            }
            return smlist;
        }


        public List<SimpleMarket> GetSearchResult(string keyword)
        {
            keyword = keyword.Replace(" ", " or ").Replace("'", "");
            List<SimpleMarket> smlist = new List<SimpleMarket>();
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {
                    var mlist = _dc.SP_GetFullTextResult("", "", keyword, 0, 0, 0);

                    if (mlist != null)
                    {
                        foreach (var m in mlist)
                        {

                            SimpleMarket sm = new SimpleMarket()
                            {
                                MKGUID = m.MKguid,
                                EDATE = m.MKedate,
                                SDATE = m.MKsdate,
                                PRICE = m.MKprice,
                                SHOPPRICE = m.MKshowprice,
                                TITLE = m.MKtitle,
                                STATUS = true,
                                COLORTAGID = m.MKcolortagid.HasValue ? m.MKcolortagid.Value : 0,
                                GROUPTAGID = m.MKgrouptagid.HasValue ? m.MKgrouptagid.Value : 0,
                                SN = m.MKsn.Trim(),
                                TYPE = (Config.MarketType)m.MKtype
                            };
                            sm.COLORTITLE = sm.COLORTAGID == 0 ? string.Empty : _dc.UTag.FirstOrDefault(ss => ss.TAGid == m.MKcolortagid).TAGtitle;
                            sm.GROUPTITLE = sm.GROUPTAGID == 0 ? string.Empty : _dc.UTag.FirstOrDefault(ss => ss.TAGid == m.MKgrouptagid).TAGtitle;

                            var pdlist = _dc.UProduct.Where(pp => pp.PDmkguid == m.MKguid).FirstOrDefault();

                            if (pdlist != null)
                            {
                                sm.IMG = pdlist.PDpicbig;
                            }
                            smlist.Add(sm);
                        }
                    }

                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "GetSearchResult", ex.ToString());
                }
                return smlist;
            }
        }

        public List<Specification> GetProductSpec(Guid _pdguid)
        {
            List<Specification> splist = new List<Specification>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var m = _dc.USpecification.Where(s => s.SPpdguid == _pdguid);
                    foreach (var sp in m)
                    {
                        Specification s = new Specification();
                        s.SID = sp.SPguid;
                        s.STOCK = sp.SPstock;
                        var t = _dc.UTag.SingleOrDefault(ss => ss.TAGid == sp.SPtitletagid && ss.TAGtype == (int)Config.TagType.SPEC);
                        s.TITLETAGID = (int)t.TAGparentid.Value;
                        s.TITLE = t.TAGtitle;
                        splist.Add(s);
                    }
                }
            }

            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetProductSpec", ex.ToString());
            }
            return splist;
        }

        public NormalMarket GetNormalMarketDetail(Guid mkguid)
        {
            try
            {

                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var m = _dc.UMarket.SingleOrDefault(mm => mm.MKguid == mkguid && mm.MKtype==(int)Config.MarketType.Normal);
                    //var mtlist = _dc.V_MarketTagList.Where(m => m.TMtagid == tagid);
                    if (m != null)
                    {
                        NormalMarket nm = new NormalMarket()
                          {
                              MKGUID = m.MKguid,
                              EDATE = m.MKedate,
                              SDATE = m.MKsdate,
                              PRICE = m.MKprice,
                              SHOPPRICE = m.MKshowPrice,
                              TITLE = m.MKtitle,
                              STATUS = m.MKstatus,
                              TAGID = m.UTagMarket.FirstOrDefault(t=>t.TMstatus && t.UTag.TAGtype==(int)Config.TagType.CLASS).TMtagid,
                              COLORTAGID = m.MKcolortagid.HasValue ? m.MKcolortagid.Value : 0,
                              GROUPTAGID = m.MKgrouptagid.HasValue ? m.MKgrouptagid.Value : 0,
                              INTRODUCTION = m.MKintroduction,
                              WASHNOTE = m.MKwashnote,
                              SHOPPINGNOTE = m.MKshoppingnote,
                              STANDARDS = m.MKspec,
                              SN = m.MKsn.Trim(),
                              DATEIN = m.MKdatein,
                              LASTMODIFY = m.MKlastmodify,
                              TYPE=Config.MarketType.Normal
                          };
                        nm.COLORTITLE = nm.COLORTAGID == 0 ? string.Empty : _dc.UTag.SingleOrDefault(ss => ss.TAGid == m.MKcolortagid).TAGtitle;
                        nm.GROUPTITLE = nm.GROUPTAGID == 0 ? string.Empty : _dc.UTag.SingleOrDefault(ss => ss.TAGid == m.MKgrouptagid).TAGtitle;

                        var mkimg = _dc.UMarketPicture.Where(mp => mp.MPmkguid == mkguid);

                        List<MarketImage> milist = new List<MarketImage>();
                        foreach (var mk in mkimg)
                        {
                            MarketImage mi = new MarketImage()
                            {
                                MPID = mk.MPid,
                                MPMKGUID = mk.MPmkguid,
                                TITLE = mk.MPnote,
                                PATH = mk.MPpath,
                                SEQUENCE = mk.MPsequence.HasValue ? mk.MPsequence.Value : 0
                            };
                            milist.Add(mi);
                        }
                        milist = milist.OrderBy(a => a.SEQUENCE).ToList();
                        nm.MARKETIMG = milist;
                        var pdlist = _dc.UProduct.Where(pp => pp.PDmkguid == m.MKguid&&pp.PDstatus);
                        List<Product> plist = new List<Product>();
                        if (pdlist != null)
                        {
                            foreach (var p in pdlist)
                            {
                                Product pt = new Product()
                                {
                                    PDGUID = p.PDguid,
                                    PICBIG = p.PDpicbig,
                                    PICSMALL = p.PDpicsmall,
                                };
                                var t = _dc.UTag.SingleOrDefault(ss => ss.TAGid == p.PDtitletagid && ss.TAGtype == (int)Config.TagType.PRODUCT);
                                pt.TITLE = t.TAGtitle;
                                pt.TITLETAGID = (int)t.TAGparentid.Value;
                                plist.Add(pt);
                            }
                        }
                        if (plist.FirstOrDefault() != null)
                            nm.IMG = plist.FirstOrDefault().PICBIG;                        
                        nm.PRODUCTLIST = plist;
                        return nm;
                    }
                    else
                        return null;
                }
            }

            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetMarket", ex.ToString());
                return null;
            }
        }

        public NormalMarket GetNormalMarketDetail(string mksn)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var m = _dc.UMarket.SingleOrDefault(mm => mm.MKsn == mksn && mm.MKtype == (int)Config.MarketType.Normal);
                    //var mtlist = _dc.V_MarketTagList.Where(m => m.TMtagid == tagid);
                    if (m != null)
                    {
                        NormalMarket nm = new NormalMarket()
                        {
                            MKGUID = m.MKguid,
                            EDATE = m.MKedate,
                            SDATE = m.MKsdate,
                            PRICE = m.MKprice,
                            SHOPPRICE = m.MKshowPrice,
                            TITLE = m.MKtitle,
                            STATUS = m.MKstatus,
                            TAGID = m.UTagMarket.FirstOrDefault(t => t.TMstatus && t.UTag.TAGtype == (int)Config.TagType.CLASS).TMtagid,
                            COLORTAGID = m.MKcolortagid.HasValue ? m.MKcolortagid.Value : 0,
                            GROUPTAGID = m.MKgrouptagid.HasValue ? m.MKgrouptagid.Value : 0,
                            INTRODUCTION = m.MKintroduction,
                            WASHNOTE = m.MKwashnote,
                            SHOPPINGNOTE = m.MKshoppingnote,
                            STANDARDS = m.MKspec,
                            SN = m.MKsn.Trim(),
                            TYPE = Config.MarketType.Normal
                        };
                        nm.COLORTITLE = nm.COLORTAGID == 0 ? string.Empty : _dc.UTag.SingleOrDefault(ss => ss.TAGid == m.MKcolortagid).TAGtitle;
                        nm.GROUPTITLE = nm.GROUPTAGID == 0 ? string.Empty : _dc.UTag.SingleOrDefault(ss => ss.TAGid == m.MKgrouptagid).TAGtitle;

                        var mkimg = _dc.UMarketPicture.Where(mp => mp.MPmkguid == nm.MKGUID);

                        List<MarketImage> milist = new List<MarketImage>();
                        foreach (var mk in mkimg)
                        {
                            MarketImage mi = new MarketImage()
                            {
                                MPID = mk.MPid,
                                MPMKGUID = mk.MPmkguid,
                                TITLE = mk.MPnote,
                                PATH = mk.MPpath,
                                SEQUENCE = mk.MPsequence.HasValue ? mk.MPsequence.Value : 0
                            };
                            milist.Add(mi);
                        }
                        nm.MARKETIMG = milist;
                        var pdlist = _dc.UProduct.Where(pp => pp.PDmkguid == nm.MKGUID && pp.PDstatus);
                        List<Product> plist = new List<Product>();
                        if (pdlist != null)
                        {
                            foreach (var p in pdlist)
                            {
                                Product pt = new Product()
                                {
                                    PDGUID = p.PDguid,
                                    PICBIG = p.PDpicbig,
                                    PICSMALL = p.PDpicsmall,
                                };
                                var t = _dc.UTag.SingleOrDefault(ss => ss.TAGid == p.PDtitletagid && ss.TAGtype == (int)Config.TagType.PRODUCT);
                                pt.TITLE = t.TAGtitle;
                                pt.TITLETAGID = (int)t.TAGparentid.Value;
                                
                                plist.Add(pt);
                            }
                            if(plist.Count>0)
                                nm.IMG = plist.FirstOrDefault().PICBIG;
                        }
                        
                        nm.PRODUCTLIST = plist;
                        return nm;
                    }
                    else
                        return null;
                }
            }

            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetMarket", ex.ToString());
                return null;
            }
        }

        public MagazineMarket GetMagazineMarketDetail(Guid mkguid)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var m = _dc.UMarket.SingleOrDefault(mm => mm.MKguid == mkguid && mm.MKtype == (int)Config.MarketType.Mangazine);
                    //var mtlist = _dc.V_MarketTagList.Where(m => m.TMtagid == tagid);
                    if (m != null)
                    {
                        MagazineMarket nm = new MagazineMarket()
                        {
                            MKGUID = m.MKguid,
                            EDATE = m.MKedate,
                            SDATE = m.MKsdate,
                            PRICE = m.MKprice,
                            SHOPPRICE = m.MKshowPrice,
                            OLDPRICE = m.MKprice,
                            NEWPRICE = m.MKshowPrice,
                            TITLE = m.MKtitle,
                            STATUS = m.MKstatus,
                            TAGID = m.UTagMarket.FirstOrDefault(t => t.TMstatus).TMtagid,
                            COLORTAGID = m.MKcolortagid.HasValue ? m.MKcolortagid.Value : 0,
                            GROUPTAGID = m.MKgrouptagid.HasValue ? m.MKgrouptagid.Value : 0,
                            INTRODUCTION = m.MKintroduction,
                            SN = m.MKsn.Trim(),
                            DATEIN = m.MKdatein,
                            LASTMODIFY = m.MKlastmodify,
                            TYPE = Config.MarketType.Mangazine
                        };

                        var mkimg = _dc.UMarketPicture.Where(mp => mp.MPmkguid == mkguid);

                        List<MarketImage> milist = new List<MarketImage>();
                        foreach (var mk in mkimg)
                        {
                            MarketImage mi = new MarketImage()
                            {
                                MPID = mk.MPid,
                                MPMKGUID = mk.MPmkguid,
                                TITLE = mk.MPnote,
                                PATH = mk.MPpath,
                                SEQUENCE = mk.MPsequence.HasValue ? mk.MPsequence.Value : 0
                            };
                            milist.Add(mi);
                        }
                        nm.MARKETIMG = milist;
                        var pdlist = _dc.UProduct.Where(pp => pp.PDmkguid == m.MKguid && pp.PDstatus);
                        List<Product> plist = new List<Product>();
                        if (pdlist != null)
                        {
                            foreach (var p in pdlist)
                            {
                                Product pt = new Product()
                                {
                                    PDGUID = p.PDguid,
                                    PICBIG = p.PDpicbig,
                                    PICSMALL = p.PDpicsmall,
                                };
                                var t = _dc.UTag.SingleOrDefault(ss => ss.TAGid == p.PDtitletagid && ss.TAGtype == (int)Config.TagType.PRODUCT);
                                pt.TITLE = t.TAGtitle;
                                pt.TITLETAGID = (int)t.TAGparentid.Value;
                                plist.Add(pt);
                            }
                        }
                        if (plist.FirstOrDefault() != null)
                            nm.IMG = plist.FirstOrDefault().PICBIG;
                        nm.PRODUCTLIST = plist;
                        return nm;
                    }
                    else
                        return null;
                }
            }

            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetMaganizeMarketDetail", ex.ToString());
                return null;
            }
        }

        public MagazineMarket GetMagazineMarketDetail(string mksn)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var m = _dc.UMarket.SingleOrDefault(mm => mm.MKsn == mksn && mm.MKtype == (int)Config.MarketType.Mangazine);
                    //var mtlist = _dc.V_MarketTagList.Where(m => m.TMtagid == tagid);
                    if (m != null)
                    {
                        MagazineMarket nm = new MagazineMarket()
                        {
                            MKGUID = m.MKguid,
                            EDATE = m.MKedate,
                            SDATE = m.MKsdate,
                            PRICE = m.MKprice,
                            SHOPPRICE = m.MKshowPrice,
                            OLDPRICE = m.MKprice,
                            NEWPRICE = m.MKshowPrice,
                            TITLE = m.MKtitle,
                            STATUS = m.MKstatus,
                            TAGID = m.UTagMarket.FirstOrDefault(t => t.TMstatus).TMtagid,
                            COLORTAGID = m.MKcolortagid.HasValue ? m.MKcolortagid.Value : 0,
                            GROUPTAGID = m.MKgrouptagid.HasValue ? m.MKgrouptagid.Value : 0,
                            INTRODUCTION = m.MKintroduction,
                            SN = m.MKsn.Trim(),
                            TYPE = Config.MarketType.Mangazine
                        };

                        var mkimg = _dc.UMarketPicture.Where(mp => mp.MPmkguid == nm.MKGUID);

                        List<MarketImage> milist = new List<MarketImage>();
                        foreach (var mk in mkimg)
                        {
                            MarketImage mi = new MarketImage()
                            {
                                MPID = mk.MPid,
                                MPMKGUID = mk.MPmkguid,
                                TITLE = mk.MPnote,
                                PATH = mk.MPpath,
                                SEQUENCE = mk.MPsequence.HasValue ? mk.MPsequence.Value : 0
                            };
                            milist.Add(mi);
                        }
                        nm.MARKETIMG = milist;
                        var pdlist = _dc.UProduct.Where(pp => pp.PDmkguid == nm.MKGUID && pp.PDstatus);
                        List<Product> plist = new List<Product>();
                        if (pdlist != null)
                        {
                            foreach (var p in pdlist)
                            {
                                Product pt = new Product()
                                {
                                    PDGUID = p.PDguid,
                                    PICBIG = p.PDpicbig,
                                    PICSMALL = p.PDpicsmall,
                                };
                                var t = _dc.UTag.SingleOrDefault(ss => ss.TAGid == p.PDtitletagid && ss.TAGtype == (int)Config.TagType.PRODUCT);
                                pt.TITLE = t.TAGtitle;
                                pt.TITLETAGID = (int)t.TAGparentid.Value;

                                plist.Add(pt);
                            }
                            if (plist.Count > 0)
                                nm.IMG = plist.FirstOrDefault().PICBIG;
                        }

                        nm.PRODUCTLIST = plist;
                        return nm;
                    }
                    else
                        return null;
                }
            }

            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetMaganizeMarketDetail", ex.ToString());
                return null;
            }
        }


        public bool SetSpecStock(Guid scguid, Guid mokguid, Guid mkguid, Guid specid, int amount)
        {
            bool isck = false;
            try
            {

                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    int? k = 0;
                    _dc.SP_SetSpecStock(scguid, mokguid, mkguid, specid, amount, ref k);
                    if (k != 0)
                    {
                        _log.RecordExceptionFormat(_log.DBExceptionPath, "[SetSpecStock]", scguid + " || " + mkguid + "||" + specid + "||" + amount + "||k:" + k);
                    }
                    else
                        isck = true;
                }
            }

            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "SetSpecStock", ex.ToString());

            }
            return isck;
        }

        public bool FreeSpecStock(Guid scguid)
        {
            bool isck = false;
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {
                    int? k = 0;
                    _dc.SP_SetSpecFreeStock(scguid, ref k);

                    if (k != 0)
                    {
                        _log.RecordExceptionFormat(_log.DBExceptionPath, "[FreeStock]", scguid + "||k:" + k);
                    }
                    else
                        isck = true;
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "FreeSpecStock", ex.ToString());
                }
            }
            return isck;
        }

        public bool FreeSpecStock(Guid scguid, Guid mokguid)
        {
            bool isck = false;
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {

                    int? k = 0;
                    //_logger.RecordException(_logger.DBExceptionPath, "[FreeSpecStockMo]" + scguid + "||" + mokguid);
                    _dc.SP_SetSpecFreeStockOne(scguid, mokguid, ref k);

                    if (k != 0)
                    {
                        _log.RecordExceptionFormat(_log.DBExceptionPath, "[FreeSpecStockMo]", scguid + "||" + mokguid + "||k:" + k);
                    }
                    else
                        isck = true;
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "FreeSpecStockMo", ex.ToString());
                }
            }
            return isck;
        }

        public IMarketSalesKit GetMarketSalesKit(Guid _mkguid, int buyamount, int total)
        {
            return null;
        }

        public void UpdateSpecStock(Guid spguid, int newstock)
        {
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {
                    var sp = _dc.USpecification.SingleOrDefault(s => s.SPguid == spguid);
                    if (sp != null)
                    {
                        sp.SPstock = newstock;
                        _dc.SubmitChanges();
                    }
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "UpdateSpecStock", ex.ToString());
                }
            }
        }

        public Guid CreateNewMarket(string MarketSN, string MarketTitle, long MarketTagid, int MarketShowprice, int MarketPrice,
                                    DateTime? Sdate, DateTime? Edate, string MarketWashnote, string MarketShoppingnote, string MarketSpec,
                                    string MarketIntrodution, long grouptagid, long colortagid, Config.MarketType mtype)
        {
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {
                    UMarket mk = new UMarket();
                    mk.MKcolortagid = (int)colortagid;
                    mk.MKdatein = DateTime.Now;
                    mk.MKedate = Edate.HasValue?Edate.Value:DateTime.Parse("2022/10/27");
                    mk.MKgrouptagid = (int)grouptagid;
                    mk.MKguid = Guid.NewGuid();
                    mk.MKintroduction = MarketIntrodution??"";
                    mk.MKlastmodify = DateTime.Now;
                    mk.MKprice = MarketPrice;
                    mk.MKsdate = Sdate.HasValue ? Sdate.Value : DateTime.Now;
                    mk.MKshoppingnote = MarketShoppingnote??"";
                    mk.MKshowPrice = MarketShowprice;
                    mk.MKsn = MarketSN;
                    mk.MKspec = MarketSpec??"";
                    mk.MKstatus = true;
                    mk.MKtitle = MarketTitle;
                    mk.MKwashnote = MarketWashnote??"";
                    mk.MKtype = (int)mtype;
                    _dc.UMarket.InsertOnSubmit(mk);
                    _dc.SubmitChanges();
                    UTagMarket tm = new UTagMarket();
                    tm.TMmkguid = mk.MKguid;
                    tm.TMtagid = MarketTagid;
                    tm.TMstatus = true;
                    _dc.UTagMarket.InsertOnSubmit(tm);
                    _dc.SubmitChanges();
                    return mk.MKguid;
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "CreateNewMarket", ex.ToString());
                }
                return Guid.Empty;
            }
        }

        public Guid CreateNewProduct(Guid mkguid, long ProductColorTagid, string ProductBig, string ProductSmall)
        {
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {
                    UProduct mp = new UProduct();
                    mp.PDdatein = DateTime.Now;
                    mp.PDguid = Guid.NewGuid();
                    mp.PDlastmodify = DateTime.Now;
                    mp.PDmkguid = mkguid;
                    mp.PDpicbig = ProductBig;
                    mp.PDpicsmall = ProductSmall;
                    mp.PDstatus = true;
                    mp.PDtitletagid = (int)ProductColorTagid;
                    _dc.UProduct.InsertOnSubmit(mp);
                    _dc.SubmitChanges();
                    return mp.PDguid;
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "CreateNewProduct", ex.ToString());
                }
                return Guid.Empty;
            }
        }

        public Guid CreateNewSpec(Guid pdguid, long titletagid, int stock)
        {
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {
                    USpecification ms = new USpecification();
                    ms.SPguid = Guid.NewGuid();
                    ms.SPpdguid = pdguid;
                    ms.SPstatus = true;
                    ms.SPstock = stock;
                    ms.SPtitletagid = (int)titletagid;
                    _dc.USpecification.InsertOnSubmit(ms);
                    _dc.SubmitChanges();
                    return ms.SPguid;
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "CreateNewSpec", ex.ToString());
                }
                return Guid.Empty;
            }
        }

        public void ImportMarketPic(string mksn, string picpath, string picnote)
        {
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {
                    
                    UMarketPicture mp = _dc.UMarketPicture.FirstOrDefault(pp => pp.UMarket.MKsn == mksn && pp.MPpath == picpath);
                    if (mp != null)
                        mp.MPnote = picnote??"";
                    else
                    {
                        var mk = _dc.UMarket.FirstOrDefault(m => m.MKsn == mksn);
                        if (mk != null)
                        {
                            mp = new UMarketPicture();
                            mp.MPmkguid = mk.MKguid;
                            mp.MPnote = picnote ?? "";
                            mp.MPpath = picpath;
                            _dc.UMarketPicture.InsertOnSubmit(mp);
                        }
                    }
                    _dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "ImportMarketPic", ex.ToString());
                }
            }
        }

        public void UpdateMarket(Guid mkguid, string MarketTitle, long MarketTagid, int MarketShowprice, int MarketPrice, DateTime? MarketSdate, 
                    DateTime? MarketEdate, string MarketWashnote, string MarketShoppingnote, string MarketSpec, string MarketIntrodution, long grouptagid, long colortagid)
        {
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {
                    UMarket uk = _dc.UMarket.FirstOrDefault(k => k.MKguid == mkguid);
                    if (uk != null)
                    {
                        uk.MKtitle = MarketTitle;
                        uk.MKshowPrice = MarketShowprice;
                        uk.MKprice = MarketPrice;
                        uk.MKsdate = MarketSdate.HasValue ? MarketSdate.Value : DateTime.Now;
                        uk.MKedate = MarketEdate.HasValue ? MarketEdate.Value : DateTime.Parse("2022/10/27") ;
                        uk.MKwashnote = MarketWashnote??"";
                        uk.MKshoppingnote = MarketShoppingnote??"";
                        uk.MKspec = MarketSpec??"";
                        uk.MKintroduction = MarketIntrodution??"";
                        uk.MKgrouptagid = (int)grouptagid;
                        uk.MKcolortagid = (int)colortagid;
                        uk.MKlastmodify = DateTime.Now;

                        var tk = _dc.UTagMarket.SingleOrDefault(ut=>ut.TMmkguid==uk.MKguid && ut.TMtagid==MarketTagid);
                        if (tk == null)
                        {
                            _dc.UTagMarket.DeleteAllOnSubmit(_dc.UTagMarket.Where(ut => ut.TMmkguid == uk.MKguid && ut.UTag.TAGtype == (int)Config.TagType.CLASS));
                            _dc.SubmitChanges();
                            tk = new UTagMarket();
                            tk.TMmkguid = uk.MKguid;
                            tk.TMstatus = true;
                            tk.TMtagid = MarketTagid;
                            _dc.UTagMarket.InsertOnSubmit(tk);
                        }
                        _dc.SubmitChanges();
                    }
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "UpdateMarket", ex.ToString());
                }
            }
        }

        public void UpdateProduct(Product product)
        {
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {
                    UProduct up = _dc.UProduct.FirstOrDefault(k => k.PDguid == product.PDGUID);
                    if (up != null)
                    {
                        up.PDlastmodify = DateTime.Now;
                        up.PDpicbig = product.PICBIG;
                        up.PDpicsmall = product.PICSMALL;
                        up.PDstatus = true;
                        var t = _dc.UTag.FirstOrDefault(ss => ss.TAGparentid == product.TITLETAGID && ss.TAGtype == (int)Config.TagType.PRODUCT);
                        up.PDtitletagid = (int)t.TAGid;
                        _dc.SubmitChanges();
                    }
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "UpdateProduct", ex.ToString());
                }
            }
        }

        public void UpdateProduct(Product product, int pdtitletagid)
        {
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {
                    UProduct up = _dc.UProduct.FirstOrDefault(k => k.PDguid == product.PDGUID);
                    if (up != null)
                    {
                        up.PDlastmodify = DateTime.Now;
                        up.PDpicbig = product.PICBIG;
                        up.PDpicsmall = product.PICSMALL;
                        up.PDstatus = true;

                        up.PDtitletagid = pdtitletagid;
                        _dc.SubmitChanges();
                    }
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "UpdateProduct", ex.ToString());
                }
            }
        }

        public Guid GetProductId(Guid specid)
        {
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {
                    var m = _dc.USpecification.SingleOrDefault(s => s.SPguid == specid);
                    
                    if (m != null)
                    {
                        return m.SPpdguid;                        
                    }
                    
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "CreateNewSpec", ex.ToString());
                }
                return Guid.Empty;
            }
        }



        public void CreateMarketPic(Guid mkguid, string note, string pic, int sequence)
        {
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {
                    UMarketPicture mp = new UMarketPicture();
                    mp.MPmkguid = mkguid;
                    mp.MPnote = note;
                    mp.MPpath = pic;
                    mp.MPsequence = sequence;
                    _dc.UMarketPicture.InsertOnSubmit(mp);
                    _dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "CreateMarketPic", ex.ToString());
                }
            }
        }

        public List<ManagerSimpleMarket> GetMarketListForAdmin(long tagid, string query, bool ismaga)
        {
            List<ManagerSimpleMarket> smlist = new List<ManagerSimpleMarket>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var mtlist = from ml in _dc.V_MarketList
                                 join tr in _dc.V_TagMarketTree on ml.MKguid equals tr.vmkguid
                                 where (tr.vtid1 == tagid || tr.vtid2 == tagid) && ml.TMtagid == tagid
                                 && (ml.MKsn.Contains(query)||ml.MKtitle.Contains(query))
                                 select new
                                 {
                                     ml.MKguid,
                                     ml.MKedate,
                                     ml.MKsdate,
                                     ml.MKprice,
                                     ml.MKshowPrice,
                                     ml.MKtitle,
                                     ml.TMtagid,
                                     ml.MKcolortagid,
                                     ml.MKgrouptagid,
                                     ml.MKsn,
                                     ml.MKtype,
                                     ml.MKstatus
                                 };
                    if(tagid==0)
                        mtlist = from ml in _dc.V_MarketList join tg in _dc.UTag on ml.TMtagid equals tg.TAGid
                                join tr in _dc.V_TagMarketTree on ml.MKguid equals tr.vmkguid
                                 where tg.TAGtype == (int)Config.TagType.CLASS && (ml.MKsn.Contains(query) || ml.MKtitle.Contains(query))
                                select new { ml.MKguid, ml.MKedate, ml.MKsdate, ml.MKprice, ml.MKshowPrice, ml.MKtitle, ml.TMtagid, ml.MKcolortagid, ml.MKgrouptagid, ml.MKsn, ml.MKtype, ml.MKstatus };
                    if (mtlist != null)
                    {
                        if (ismaga)
                            mtlist = mtlist.Where(m => m.MKtype == (int)Config.MarketType.Mangazine);
                        else
                            mtlist = mtlist.Where(m => m.MKtype != (int)Config.MarketType.Mangazine);

                        foreach (var mt in mtlist)
                        {
                            ManagerSimpleMarket sm = new ManagerSimpleMarket()
                            {
                                MKGUID = mt.MKguid,
                                EDATE = mt.MKedate,
                                SDATE = mt.MKsdate,
                                PRICE = mt.MKprice,
                                SHOPPRICE = mt.MKshowPrice,
                                TITLE = mt.MKtitle,
                                STATUS = mt.MKstatus,
                                TAGID = mt.TMtagid,
                                COLORTAGID = mt.MKcolortagid.HasValue ? mt.MKcolortagid.Value : 0,
                                GROUPTAGID = mt.MKgrouptagid.HasValue ? mt.MKgrouptagid.Value : 0,
                                SN = mt.MKsn.Trim(),
                                TYPE = (Config.MarketType)mt.MKtype
                            };
                            sm.COLORTITLE = sm.COLORTAGID == 0 ? string.Empty : _dc.UTag.FirstOrDefault(ss => ss.TAGid == mt.MKcolortagid).TAGtitle;
                            sm.GROUPTITLE = sm.GROUPTAGID == 0 ? string.Empty : _dc.UTag.FirstOrDefault(ss => ss.TAGid == mt.MKgrouptagid).TAGtitle;

                            var pdlist = _dc.UProduct.Where(pp => pp.PDmkguid == mt.MKguid && pp.PDstatus).Select(p=>new{p.PDtitletagid,p.PDguid});
                            List<ManagerSimpleProduct> plist = new List<ManagerSimpleProduct>();
                            if (pdlist != null)
                            {
                                foreach (var p in pdlist)
                                {
                                    ManagerSimpleProduct pt = new ManagerSimpleProduct();
                                    var t = _dc.UTag.SingleOrDefault(ss => ss.TAGid == p.PDtitletagid && ss.TAGtype == (int)Config.TagType.PRODUCT);
                                    if (t != null)
                                    {
                                        pt.PNAME = t.TAGtitle;
                                    }
                                    List<ManagerSimpleSpec> spl = new List<ManagerSimpleSpec>();
                                    var splist = _dc.USpecification.Where(ss => ss.SPpdguid == p.PDguid && ss.SPstatus).Select(ss => new { ss.SPtitletagid, ss.SPstock });
                                    foreach (var s in splist)
                                    {
                                        ManagerSimpleSpec sp = new ManagerSimpleSpec();
                                        sp.SSTOCK = s.SPstock;
                                        var st = _dc.UTag.SingleOrDefault(ss => ss.TAGid == s.SPtitletagid && ss.TAGtype == (int)Config.TagType.SPEC);
                                        if (st != null)
                                        {
                                            sp.SSNAME = st.TAGtitle;
                                        }
                                        spl.Add(sp);
                                    }
                                    pt.MSSLIST = spl;
                                    plist.Add(pt);
                                }
                            }
                            sm.MSPLIST = plist;
                            smlist.Add(sm);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetMarketListForAdmin", ex.ToString());
            }
            return smlist;
        }

        public bool UpdateMarketStatus(Guid mkguid)
        {
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {
                    var market = _dc.UMarket.SingleOrDefault(m => m.MKguid == mkguid);
                    if (market != null)
                    {
                        market.MKstatus = !market.MKstatus;
                        _dc.SubmitChanges();

                        return true;
                    }
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "UpdateMarketStatus", ex.ToString());
                }
                return false;
            }
        }

        public void UpdateProductStatus(Product product)
        {
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {
                    UProduct up = _dc.UProduct.FirstOrDefault(k => k.PDguid == product.PDGUID);
                    if (up != null)
                    {
                        up.PDlastmodify = DateTime.Now;
                        up.PDstatus = product.PDSTATUS;
                        _dc.SubmitChanges();
                    }
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "UpdateProductStatus", ex.ToString());
                }
            }
        }


        public MarketImage GetMarketImage(int mpid)
        {
            MarketImage mi = new MarketImage();
            try
            {

                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    UMarketPicture ump = _dc.UMarketPicture.FirstOrDefault(mp => mp.MPid == mpid);
                    if (ump != null)
                    {
                        mi.MPID = ump.MPid;
                        mi.MPMKGUID = ump.MPmkguid;
                        mi.SEQUENCE = ump.MPsequence;
                        mi.PATH = ump.MPpath;
                        mi.TITLE = ump.MPnote;
                        
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetMarketImage", ex.ToString());
            }
            return mi;
        }

        public void UpdateMarketImage(MarketImage mi)
        {
            using (_dc = new DataClassesMenDataContext(Config.connString))
            {
                try
                {
                    UMarketPicture umi = _dc.UMarketPicture.FirstOrDefault(a => a.MPid == mi.MPID);
                    if (umi != null)
                    {
                        umi.MPnote = mi.TITLE;
                        umi.MPpath = mi.PATH;
                        umi.MPsequence = mi.SEQUENCE;
                        
                        _dc.SubmitChanges();
                    }
                }
                catch (Exception ex)
                {
                    _log.RecordExceptionFormat(_log.DBExceptionPath, "UpdateMarketImage", ex.ToString());
                }
            }
        }


        
    }
}
